package com.heima.datasync.service.impl;

import com.heima.common.exception.CustException;
import com.heima.datasync.mapper.ApArticleMapper;
import com.heima.datasync.service.EsDataService;
import com.heima.es.service.EsService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.search.vos.SearchArticleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class EsDataServiceImpl implements EsDataService {

    @Autowired
    private ApArticleMapper apArticleMapper;

    @Autowired
    private EsService<SearchArticleVO> esService;


    //索引库的名称
    public static final String ARTICLE_INDEX_NAME = "app_info_article";

    /**
     * 创建索引库
     * @return
     */
    @Override
    public ResponseResult dataInit() {

        //判断索引库是否存在
        boolean isIndex = esService.existIndex(ARTICLE_INDEX_NAME);
        //如果索引库存在  就删除
        if (isIndex){
            esService.deleteIndex(ARTICLE_INDEX_NAME);
        }

        //创建新的索引库
        //前面的是索引库对应的字段   后面是索引库的名称
        esService.createIndex(
                "{\n" +
                        "    \"mappings\":{\n" +
                        "        \"properties\":{\n" +
                        "            \"id\":{\n" +
                        "                \"type\":\"long\"\n" +
                        "            },\n" +
                        "            \"publishTime\":{\n" +
                        "                \"type\":\"date\"\n" +
                        "            },\n" +
                        "            \"layout\":{\n" +
                        "                \"type\":\"integer\"\n" +
                        "            },\n" +
                        "            \"images\":{\n" +
                        "                \"type\":\"keyword\",\n" +
                        "                \"index\": false\n" +
                        "            },\n" +
                        "           \"staticUrl\":{\n" +
                        "                \"type\":\"keyword\",\n" +
                        "                \"index\": false\n" +
                        "            },\n" +
                        "            \"authorId\": {\n" +
                        "          \t\t\"type\": \"long\"\n" +
                        "       \t\t},\n" +
                        "          \"title\":{\n" +
                        "            \"type\":\"text\",\n" +
                        "            \"analyzer\":\"ik_smart\"\n" +
                        "          }\n" +
                        "        }\n" +
                        "    }\n" +
                        "}",ARTICLE_INDEX_NAME
        );
        //批量插入数据
        //查询数据库所有的文章
        List<ApArticle> allArticlesList = apArticleMapper.findAllArticles();
        if (allArticlesList == null || allArticlesList.size() == 0){
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST,"数据库中没有数据");
        }
        //遍历集合 将数据插入到es中
        List<SearchArticleVO> list = new ArrayList<>();
        for (ApArticle apArticle : allArticlesList) {
            //封装数据
            SearchArticleVO searchArticleVO = new SearchArticleVO();
            BeanUtils.copyProperties(apArticle,searchArticleVO);
            list.add(searchArticleVO);
        }
        //插入数据到索引库中
        esService.saveBatch(list,ARTICLE_INDEX_NAME);
        //返回数据
        return ResponseResult.okResult();
    }
}
